package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import model.Candidato;
import utilities.Constants;
import utilities.ImageRenderer;
import utilities.Utils;
import controller.ButtonColumn;
import controller.StartController;
import controller.VotacionController;

public class VotacionView extends JFrame implements GenericView{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8168503252903755527L;
	private JPanel contentPane;
	private VotacionController controller;
	private static VotacionView instance = null;
	private DefaultTableModel tableAttributes;
	private JTable votacionTable;


	/**
	 * Create the frame.
	 */
	public VotacionView() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowListener() {
			@Override
			public void windowActivated(WindowEvent arg0) {
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				controller.handleButtonCancelar();
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
        });
		setResizable(Boolean.FALSE);
		setBounds(Constants.SRC_X, Constants.SRC_Y, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblConfiguracin = new JLabel("Votación");
		lblConfiguracin.setBounds(342, 28, 226, 15);
		contentPane.add(lblConfiguracin);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(30, 72, 628, 443);
		contentPane.add(scrollPane);
		tableAttributes = new DefaultTableModel() {
			 /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int mColIndex) {
				if (mColIndex == 2){
					return true;
				}
			        return false;
			    }
		};
		tableAttributes.addColumn ("Nombre");
		tableAttributes.addColumn ("Foto");
		tableAttributes.addColumn("Votar");
		votacionTable = new JTable(tableAttributes);
		votacionTable.setFocusable(false);
		votacionTable.setRowSelectionAllowed(false);
		votacionTable.getColumn("Nombre").setPreferredWidth(200);
        votacionTable.setRowHeight(Constants.TABLE_HEIGHT);
		votacionTable.getColumn("Foto").setPreferredWidth(Constants.TABLE_WIDTH);
		votacionTable.getColumn("Votar").setPreferredWidth(5);
		scrollPane.setViewportView(votacionTable);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(288, 527, 117, 25);
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonCancelar();
			}
		});
		contentPane.add(btnCancelar);
	}
	
	public static VotacionView getInstance() {
		if (instance == null){
			instance = new VotacionView();
		}
		return instance;
	}

	@Override
	public void showWindow() {
		setVisible(true);
	}

	@Override
	public void hideWindow() {
		setVisible(false);
	}

	public void setController(VotacionController controller) {
		this.controller = controller;
	}

	@SuppressWarnings("unused")
	public void setList() {
		ArrayList<Candidato> candidates = (ArrayList<Candidato>) ((StartController)this.controller.getParentController()).getManager().getCandidates();
		DefaultTableModel dtm = (DefaultTableModel) this.votacionTable.getModel();
		dtm.getDataVector().removeAllElements();
		dtm.fireTableDataChanged();
		votacionTable.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer());
		for (Candidato candidato : candidates) {
			Icon icon = Utils.setImageToLabel(Constants.defaultPath + candidato.getId(), Constants.TABLE_WIDTH, Constants.TABLE_HEIGHT);
			Object[] row = {
					candidato.getName(), icon, "Votar" 
			};
		
			Action votar = new AbstractAction()
			{
			    /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e)
			    {
			        voteForCandidate();
			    }
			};
			ButtonColumn voteColumn = new ButtonColumn(votacionTable, votar, 2);
			dtm.addRow(row);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public void voteForCandidate(){
		DefaultTableModel dtm = (DefaultTableModel) this.votacionTable.getModel();
		int[] rows = this.votacionTable.getSelectedRows();
		int index = rows[0];
		String nombreCandidato = ((Vector) dtm.getDataVector()
				.elementAt(index)).elementAt(0).toString();
		this.controller.voteFor(nombreCandidato);
	}
	
}
